﻿using CoreShop.App.Units.Interface;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using Loading;
using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class frmPayAccountInfo : UIForm
    {
        PayAccountModel payAccountModel = new PayAccountModel();
        string postStr;
        string retStr;
        public bool returnValue;
        List<PayAccountModel> ListAccountModelInfo = new List<PayAccountModel>();
        OpaqueCommand cmd = new OpaqueCommand();
        public frmPayAccountInfo(PayAccountModel _payAccountModel)
        {
            InitializeComponent();
            payAccountModel = _payAccountModel;
        }

        public frmPayAccountInfo(List<PayAccountModel> ListAccount)
        {
            InitializeComponent();
            ListAccountModelInfo = ListAccount;
        }

        private bool CheckData1()
        {
            if (accountName.Text == null)
            {
                UIMessageBox.Show("请输入名称", "提示", UIStyle.Green, UIMessageBoxButtons.OK);
                return false;
            }
            if (accountName.Text == null)
            {
                UIMessageBox.Show("请输入编码", "提示", UIStyle.Green, UIMessageBoxButtons.OK);
                return false;
            }
            return true;
        }

        private void FrmEditInputs_Load(object sender, EventArgs e)
        {
            ControlsDataBinding.DataBind(payAccountModel, this, DataSourceUpdateMode.OnPropertyChanged);

            #region 加载类型
            this.channelType.Items.Clear();
            var type = EnumHelper.EnumToList<GlobalEnumVars.channelType>();
            channelType.DataSource = type;
            channelType.DisplayMember = "description";
            channelType.ValueMember = "value";
            this.channelType.SelectedValue = (int)GlobalEnumVars.channelType.Sxf;
            #endregion

            #region 加载状态
            this.accountStatus.Items.Clear();
            type = EnumHelper.EnumToList<GlobalEnumVars.accountStatus>();
            accountStatus.DataSource = type;
            accountStatus.DisplayMember = "description";
            accountStatus.ValueMember = "value";
            this.accountStatus.SelectedValue = (int)GlobalEnumVars.accountStatus.Use;
            #endregion

            #region 加载类型
            this.accountType.Items.Clear();
            type = EnumHelper.EnumToList<GlobalEnumVars.accountType>();
            accountType.DataSource = type;
            accountType.DisplayMember = "description";
            accountType.ValueMember = "value";
            this.accountType.SelectedValue = (int)GlobalEnumVars.accountType.Payment;
            #endregion
        }

        private async void DoCreate(List<PayAccountModel> ListAccountInfo)
        {
            try
            {
                cmd.ShowOpaqueLayer(pnl_Form, 125, true);
                ReturnModel returnModel = await SystemApiUtil.BatchInsertPayInfo(AppConstVars.sysUser,ListAccountInfo);

                if (!returnModel.Status.Equals(ReturnStatus.Success))
                {
                    ShowInfoTip("请稍后再试");
                    return;
                }
                ApiReturnModel vipMode = returnModel.MessageOrString.JsonToEntity<ApiReturnModel>();

                if (vipMode.code != 200)
                {
                    ShowInfoTip($"保存失败：{vipMode.msg}");
                    return;
                }
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                cmd.HideOpaqueLayer();
            }
        }

        private void SetControlClear()
        {
            foreach (Control Ctrol in this.Controls)
            {
                if (Ctrol is Sunny.UI.UITextBox)
                {
                    Ctrol.Text = "";
                }
            }
            payAccountModel.idx = 0;
        }


        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (!CheckData1()) return;
                payAccountModel.accountName = this.accountName.Text;
                payAccountModel.channelId = this.channelType.SelectedValue.ToString();
                payAccountModel.appId = this.accountName.Text;
                payAccountModel.appSecret = this.accountName.Text;
                payAccountModel.accountType = CommFunc.ConvertToInt32(this.accountType.SelectedValue);
                payAccountModel.status = CommFunc.ConvertToInt32(this.accountStatus.SelectedValue);
                payAccountModel.tenantId = AppConstVars.sysStore.corpId;
                List<PayAccountModel> PostAccountInfo = new List<PayAccountModel>();
                if (ListAccountModelInfo != null)
                {
                    payAccountModel.idx = ListAccountModelInfo.Count;
                    string postStr = ListAccountModelInfo.ToJsonString();
                    PostAccountInfo = postStr.JsonToEntity<List<PayAccountModel>>();
                    PostAccountInfo.Add(payAccountModel);
                    int index = 1;
                    PostAccountInfo.ForEach(p =>
                    {
                        p.idx = index;
                        index = index + 1;
                    });
                }
                else
                {
                    payAccountModel.idx = 1;
                    PostAccountInfo.Add(payAccountModel);
                }
                DoCreate(PostAccountInfo);
                //}
                //SetControlClear();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DoEdit()
        {
            try
            {
                string msg = "";
               
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}